package gs.eisenhower.swallet.dao;

import gs.eisenhower.swallet.db.WalletSqlHelper;
import gs.eisenhower.swallet.model.Category;
import gs.eisenhower.swallet.utils.constants.WalletConstants;
import gs.eisenhower.swallet.utils.methods.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;

public class CategoryDaoImpl extends GenericDaoImpl implements CategoryDao {
	
	public CategoryDaoImpl() {
	}
	
	@Override
	public List<String> read(Category category) {		
		QueryBuilder queryBuilder = new QueryBuilder();
		final String WHERE_STATMENT = WalletSqlHelper.FINANCE_TYPE + "='"
				+ category.getType() + WalletConstants.APOSTROPHE;
		
		queryBuilder.SELECT(WalletSqlHelper.CATEGORY_COLUMN);
		queryBuilder.FROM(WalletSqlHelper.CATEGORY_TABLE_NAME);
		queryBuilder.WHERE(WHERE_STATMENT);
			
		Cursor cursor = open().rawQuery(queryBuilder.toString(), null);
		List<String> allCategories = new ArrayList<String>();
		if (cursor.moveToFirst()) {
			allCategories.add(cursor.getString(0));
			while (cursor.moveToNext()) {
				allCategories.add(cursor.getString(0));
			}
		}
		cursor.close();
		close();

		return allCategories;
	}
	
	@Override
	public void delete(ArrayList<String> selectedItems) {
		for (String item : selectedItems) {
			QueryBuilder queryBuilder = new QueryBuilder();
			String whereStatment = WalletSqlHelper.CATEGORY_COLUMN + "='" + item
					+ WalletConstants.APOSTROPHE;
			queryBuilder.DELETE(WalletSqlHelper.CATEGORY_TABLE_NAME);
			queryBuilder.WHERE(whereStatment);
			open().execSQL(queryBuilder.toString());
		}
		close();
	}
}
